Next.js template
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

[dataId].js 808B

1234567891011121314151617181920212223242526272829303132333435
  1. const Person = require('../../../models/person');
  2. import dbConnect from '../../../utils/helpers/dbHelpers';
  3. async function handler(req, res) {
  4. const { method } = req;
  5. await dbConnect();
  6. switch (method) {
  7. case 'GET': {
  8. try {
  9. const dataId = req.query.dataId;
  10. const person = await Person.findOne({ customID: dataId });
  11. if (!person) {
  12. throw new Error('Person with this id does not exist!');
  13. }
  14. res.status(200).json({
  15. message: 'Fetch single data successfull!',
  16. singleData: person,
  17. });
  18. } catch (error) {
  19. res.status(400).json({ message: error.message });
  20. }
  21. break;
  22. }
  23. default:
  24. res.status(405).json({ message: 'Method not allowed' });
  25. break;
  26. }
  27. }
  28. export default handler;